package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwaxT;
import cn.e4j.modular.crm.mapper.GwaxTMapper;
import cn.e4j.modular.crm.service.GwaxTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shixc
 * @since 2019-01-15
 */
@Service
public class GwaxTServiceImpl extends ServiceImpl<GwaxTMapper, GwaxT> implements GwaxTService {
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getList(Page<Object> page, Dto dto) {

        List<Map<String, Object>> gwamDtos = this.baseMapper.listPage(page, dto);

        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwamList(Dto dto) {

        List<Map<String, Object>> gwamDtos = this.baseMapper.getGwamList(dto);

        return gwamDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwabList(Dto dto) {

        List<Map<String, Object>> gwabDtos = this.baseMapper.getGwabList(dto);

        return gwabDtos;
    }

    @Transactional(readOnly = true)
    public Integer getMaxGwax002(Dto dto) {

        Integer maxGwax002 = this.baseMapper.getMaxGwax002(dto);

        return maxGwax002;
    }

    @Transactional(readOnly = true)
    public GwaxT getByKey(String GWAXENT, String GWAX001, String GWAX002) {
        GwaxT gwax = this.baseMapper.getByKey(GWAXENT, GWAX001, GWAX002);
        return gwax;
    }

    @Transactional
    @Override
    public void add(GwaxT gwaxT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwaxT.setGWAXENT(shiroUser.getDBC());
        gwaxT.setGWAXUSER(shiroUser.getACCOUNT());
        gwaxT.setGWAXGRUP(shiroUser.getORG_ID());
        gwaxT.setGWAXDATE(LocalDate.now());
        gwaxT.setGWAXTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwaxT.setGWAXORIG(shiroUser.getORG_ID());
        gwaxT.setGWAXORIU(shiroUser.getACCOUNT());
        gwaxT.insert();
    }

    @Transactional
    @Override
    public void edit(GwaxT gwax) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwax.setGWAXMODU(shiroUser.getACCOUNT());
        UpdateWrapper<GwaxT> gwaxTUpdateWrapper = new UpdateWrapper<>();
        gwaxTUpdateWrapper.eq("GWAXENT", gwax.getGWAXENT());
        gwaxTUpdateWrapper.eq("GWAX001", gwax.getGWAX001());
        gwaxTUpdateWrapper.eq("GWAX002", gwax.getGWAX002());
        gwaxTUpdateWrapper.eq("GWAX003", gwax.getGWAX003());
        gwaxTUpdateWrapper.eq("GWAX008", gwax.getGWAX008());
        gwax.update(gwaxTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delete(GwaxT gwaxT) {
        UpdateWrapper<GwaxT> gwaxTUpdateWrapper = new UpdateWrapper<>();
        gwaxTUpdateWrapper.eq("GWAXENT", gwaxT.getGWAXENT());
        gwaxTUpdateWrapper.eq("GWAX001", gwaxT.getGWAX001());
        gwaxTUpdateWrapper.eq("GWAX002", gwaxT.getGWAX002());
        gwaxTUpdateWrapper.eq("GWAX003", gwaxT.getGWAX003());
        gwaxTUpdateWrapper.eq("GWAX008", gwaxT.getGWAX008());
        gwaxT.delete(gwaxTUpdateWrapper);
    }

    @Transactional
    @Override
    public void delDataBatch(List<GwaxT> gwaxTList) {
        for (GwaxT gwaxT : gwaxTList) {
            UpdateWrapper<GwaxT> gwaxTUpdateWrapper = new UpdateWrapper<>();
            gwaxTUpdateWrapper.eq("GWAXENT", gwaxT.getGWAXENT());
            gwaxTUpdateWrapper.eq("GWAX001", gwaxT.getGWAX001());
            gwaxTUpdateWrapper.eq("GWAX002", gwaxT.getGWAX002());
            gwaxTUpdateWrapper.eq("GWAX003", gwaxT.getGWAX003());
            gwaxTUpdateWrapper.eq("GWAX008", gwaxT.getGWAX008());
            gwaxT.delete(gwaxTUpdateWrapper);
        }
    }
}
